package com.ruoyi.third.service.impl;

import java.util.List;

import com.ruoyi.common.consts.DefaultValues;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.third.mapper.ThirdEventMapper;
import com.ruoyi.third.domain.ThirdEvent;
import com.ruoyi.third.service.IThirdEventService;

/**
 * 告警事件Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-02-06
 */
@Service
public class ThirdEventServiceImpl implements IThirdEventService 
{
    private static final String CACHE_MAP = "third_event";
    private static final Logger log = LoggerFactory.getLogger(ThirdEventServiceImpl.class);

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ThirdEventMapper thirdEventMapper;


    /**
     * 清除缓存
     * @return
     */
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 1;
    }
    /**
     * 查询告警事件
     *
     * @param sourceCode 原始告警代码
     * @param vendorCode 厂商代码
     * @return 告警事件
     */
    public ThirdEvent selectThirdEventByCode(String sourceCode, String vendorCode){
        String cacheKey = vendorCode+"__"+sourceCode;
        ThirdEvent thirdEvent = redisCache.getCacheMapValue(CACHE_MAP, cacheKey);
        if(thirdEvent != null){
            return thirdEvent;
        }

        List<ThirdEvent> list = thirdEventMapper.selectThirdEventList(new ThirdEvent());
        for(ThirdEvent event:list){
            redisCache.setCacheMapValue(CACHE_MAP, event.getVendorCode()+"__"+event.getSourceCode(), event);
        }
        return redisCache.getCacheMapValue(CACHE_MAP, cacheKey);
    }

    /**
     * 查询告警事件
     * 
     * @param id 告警事件主键
     * @return 告警事件
     */
    @Override
    public ThirdEvent selectThirdEventById(Long id)
    {
        return thirdEventMapper.selectThirdEventById(id);
    }

    /**
     * 查询告警事件列表
     * 
     * @param thirdEvent 告警事件
     * @return 告警事件
     */
    @Override
    public List<ThirdEvent> selectThirdEventList(ThirdEvent thirdEvent)
    {
        return thirdEventMapper.selectThirdEventList(thirdEvent);
    }

    /**
     * 新增告警事件
     * 
     * @param thirdEvent 告警事件
     * @return 结果
     */
    @Override
    public int insertThirdEvent(ThirdEvent thirdEvent)
    {
        thirdEvent.setCreateTime(DateUtils.getNowDate());
        return thirdEventMapper.insertThirdEvent(thirdEvent);
    }

    /**
     * 修改告警事件
     * 
     * @param thirdEvent 告警事件
     * @return 结果
     */
    @Override
    public int updateThirdEvent(ThirdEvent thirdEvent)
    {
        reCache();
        thirdEvent.setUpdateTime(DateUtils.getNowDate());
        return thirdEventMapper.updateThirdEvent(thirdEvent);
    }

    /**
     * 批量删除告警事件
     * 
     * @param ids 需要删除的告警事件主键
     * @return 结果
     */
    @Override
    public int deleteThirdEventByIds(Long[] ids)
    {
        reCache();
        return thirdEventMapper.deleteThirdEventByIds(ids);
    }

    /**
     * 删除告警事件信息
     * 
     * @param id 告警事件主键
     * @return 结果
     */
    @Override
    public int deleteThirdEventById(Long id)
    {
        reCache();
        return thirdEventMapper.deleteThirdEventById(id);
    }

    /**
     * 自动注册事件
     * @param eventCode
     * @param eventDesc
     * @param pfCode
     * @return
     */
    public ThirdEvent autoEvent(String eventCode, String eventDesc, String pfCode){
        ThirdEvent thirdEvent = new ThirdEvent();
        thirdEvent.setAlarmType(DefaultValues.alarmType);
        thirdEvent.setSourceCode(eventCode);
        thirdEvent.setAlarmState(ThirdEvent.STATE_CREATE);
        thirdEvent.setComment(eventDesc == null ? eventCode : eventDesc);
        thirdEvent.setVendorCode(pfCode);
        thirdEvent.setCreateBy("auto");
        insertThirdEvent(thirdEvent);
        return thirdEvent;
    }

}
